from types import MethodType

class Animal(object):
    
    def run(self):
        print("animal is running....")

class Dog(Animal):
    def run(self):
        print("Dog is running...")

class Cat(Animal):
    __slots__ = ('name', 'age')
    def run(self):
        print("Cat is running...")

    def __len__(self):
        print("本对象没有长度")
        return 100

d = Dog()
c = Cat()
d.run()
c.run()
print(isinstance(c, Cat))
print(isinstance(c, Animal))
print(isinstance(c, Dog))
print(isinstance(c, (Dog, Animal)))

print("获取对象的基本信息", dir(c))
print("对象的长度为", len(c))

print("面向对象高级特性，给创建出来的对象绑定方法")
def set_age(self, age):
    self.age = age

c.set_age = MethodType(set_age, c)
c.set_age(100)

print('c的年龄是：', c.age)
f = Cat()
print('f是否有年龄设置方法：', hasattr(f, 'set_age'))
c.city = 'Beijing'
print(c.city)


print('访问属性的方式调用方法')
class Demo(object):
    
    @property
    def age(self):
        return self._age

    @age.setter
    def age(self, age):
        self._age = age

demo = Demo()
demo.age = 10
print(demo.age)

